<template>
  <el-dialog
      :title="type === 'add' ? '添加问题' : '修改问题'"
      v-model="visible"
      width="800px"
      @close="handleClose"
  >
    <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="100px" class="menu-form">
      <el-form-item label="标题" prop="title">
        <el-input type="text" v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="ruleForm.description" type="textarea"/>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
            v-model="ruleForm.type"
            placeholder="类型"
            style="width: 240px"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优next" prop="nextTopicA">
        <el-select
            v-model="ruleForm.nextTopicA"
            filterable
            remote
            size="large"
            reserve-keyword
            placeholder="请输入题目信息"
            :remote-method="remoteMethod"
            :loading="loadingA"
        >
          <el-option
              v-for="item in topicOptions"
              :key="item.value"
              :label="item.title"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="良next" prop="nextTopicB">
        <el-select
            v-model="ruleForm.nextTopicB"
            filterable
            remote
            size="large"
            reserve-keyword
            placeholder=""
            :remote-method="remoteMethod"
            :loading="loadingB"
        >
          <el-option
              v-for="item in topicOptions"
              :key="item.value"
              :label="item.title"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="差next" prop="nextTopicC">
        <el-select
            v-model="ruleForm.nextTopicC"
            filterable
            remote
            size="large"
            reserve-keyword
            placeholder="请输入题目信息"
            :remote-method="remoteMethod"
            :loading="loadingC"
        >
          <el-option
              v-for="item in topicOptions"
              :key="item.value"
              :label="item.title"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {onMounted, reactive, ref, toRefs} from 'vue'
import {useRoute} from 'vue-router'
import axios from '@/utils/axios'
import {ElMessage} from 'element-plus'

export default {
  name: 'DialogAddRole',
  props: {
    type: String,
    reload: Function
  },
  setup: function (props) {
    const formRef = ref(null)
    const route = useRoute()
    const state = reactive({
      visible: false,
      loadingA: false,
      loadingB: false,
      loadingC: false,
      topicOptions: [],
      ruleForm: {
        title: '',
        description: '',
        type: "",
        nextTopicA: "",
        nextTopicB: "",
        nextTopicC: "",
      },
      options: [
        {label: "教学能力", value: "教学能力"},
        {label: "问题解答", value: "问题解答"},
        {label: "课堂互动", value: "课堂互动"},
        {label: "师生交流", value: "师生交流"}],
      rules: {
        title: [
          {required: 'true', message: '标题不能为空', trigger: ['change']}
        ],
        description: [
          {required: 'true', message: '描述不能为空', trigger: ['change']}
        ],
        type: [
          {required: 'true', message: '类型不能为空', trigger: ['change']}
        ],
      },
      id: ''
    })
    // 获取详情
    const getDetail = (id) => {
      axios.get(`/evaluate/topic/${id}`).then(res => {
        state.ruleForm = {...res}
      })
    }
    // 开启弹窗
    const open = (id) => {
      state.visible = true
      if (id) {
        state.id = id
        getDetail(id)
      } else {
        state.ruleForm = {
          title: '',
          description: '',
          type: "",
          nextTopicA: "",
          nextTopicB: "",
          nextTopicC: "",
        }
      }
    }
    // 关闭弹窗
    const close = () => {
      state.visible = false
    }
    const handleClose = () => {
      formRef.value.resetFields()
    }

    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          if (props.type === 'add') {
            axios.post('/evaluate/topic', {...state.ruleForm}).then(() => {
              ElMessage.success('添加成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          } else {
            axios.post('/evaluate/topic', {
              id: state.id,
              ...state.ruleForm
            }).then(() => {
              ElMessage.success('修改成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          }
        }
      })
    }

    const remoteMethod = (query) => {
      if (query) {
        state.loading = true
        axios.get("/evaluate/topic", {params: {query}}).then(res => {
          state.topicOptions = res.map(topic => ({
            id: topic.id,
            title: topic.title
          }))
          state.loading = false
        })
      } else {
        state.topicOptions = []
      }
    }

    onMounted(() => {
      state.loadingA = true
      state.loadingB = true
      state.loadingC = true
      axios.get("/evaluate/topic", {params: {}}).then(res => {
        state.topicOptions = res.map(topic => ({
          id: topic.id,
          title: topic.title
        }))
        state.loadingA = false
        state.loadingB = false
        state.loadingC = false
      })
    })

    return {
      ...toRefs(state),
      open,
      close,
      formRef,
      submitForm,
      remoteMethod,
      handleClose
    }
  }
}
</script>